package com.yanqu.road.server.cmd.activity.mammonhall;

import com.yanqu.road.entity.activity.mammonhall.MammonHallSourceDataInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.MammonHallProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.mammonhall.CrossMammonHallMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_MAMMON_HALL_RANK_LIST, desc = "财神殿-榜单")
public class CrossMammonHallRankListCmd extends CrossCommand {

    List<Integer> typeList = new ArrayList<>();
    int num;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MammonHallProto.MammonHallRankListReqMsg reqMsg = MammonHallProto.MammonHallRankListReqMsg.parseFrom(packet.getBytes());
        typeList = reqMsg.getTypeList();
        num = reqMsg.getNum();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        MammonHallProto.MammonHallRankListRespMsg.Builder respMsg = MammonHallProto.MammonHallRankListRespMsg.newBuilder();
        for (Map.Entry<Integer, List<MammonHallSourceDataInfo>> listEntry : CrossMammonHallMgr.getSourceDataMap().entrySet()) {
            if(typeList.size() > 0 && !typeList.contains(listEntry.getKey())){
                continue;
            }
            List<MammonHallSourceDataInfo> list = listEntry.getValue();
            if(list.size() == 0){
                continue;
            }
            MammonHallProto.MammonHallRankListTemp.Builder listBuilder = MammonHallProto.MammonHallRankListTemp.newBuilder();
            listBuilder.setType(listEntry.getKey());
            int i = 1;
            for (MammonHallSourceDataInfo dataInfo : list) {
                MammonHallProto.MammonHallRankTemp.Builder builder = CrossMammonHallMgr.getRankTempBuilder(dataInfo);
                UserBaseInfo ubi = CrossUserMgr.getUserBaseInfo(dataInfo.getUserId());
                if(ubi != null){
                    builder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(ubi));
                }
                listBuilder.addRank(builder);
                i++;
                if(i > num){
                    break;
                }
            }
            respMsg.addAllType(listBuilder);
        }
        respMsg.setRet(0);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_MAMMON_HALL_RANK_LIST, respMsg));
    }

    @Override
    public void sendData() {

    }

}
